﻿var GY = {};
GY.Bill = {};
GY.Bill.pageIndex = 1;
GY.Bill.pageSize = 10;
GY.Bill.page_show_cnt = 7;

function fnShade(shade) {
    var height = $(document).height();
    $(shade).height(height);
}

function ShowTip(msg) {
    $("#div_tip").show();
    $("#div_tip").text(msg);

    setTimeout(HideTip, 3000);
}

function HideTip() {
    $("#div_tip").hide();

    
}

//弹出确认框并注册事件
function ShowPopup(msg, btnName) {
    fnShade($(".shade"));
    $("#p_msg").text(msg);
    $("#btnSure").text(btnName);
    $("#div_common_shade").show();
    $("#div_common_mask_tip").show();
}

function ShowPopup(msg, btnName,command) {
    fnShade($(".shade"));
    $("#p_msg").text(msg);
    $("#btnSure").text(btnName);
    $("#btnSure").attr("command", command);
    $("#div_common_shade").show();
    $("#div_common_mask_tip").show();
}

function HidePopup() {
    $(".shade").hide();
    $(".mask").hide();
}

GY.Bill.Common = {
    //加载好友邀请信息
    LoadInvite: function (billId) {
        var $this = $("#div_member_relation");

        $this.attr("data-id", billId);

        $.ajax({
            type: "Post",
            url: $this.attr("data-url"),
            data: { billId: billId },
            dataType: "html",
            async: false,
            success: function (result) {
                if (result.trim() != "") {
                    $this.find(".nofriend").hide();
                    $this.find(".group").remove();
                    $this.find(".list").remove();
                    $this.find(".btn").before(result);
                } else {
                    $this.find(".nofriend").show();
                    $this.find(".btn").hide();
                }

                fnShade($(".shade"));
                $(".shade").show();
                $(".mask_quote").show();
            }
        });
    },
    //邀请好友报价
    InviteOffer: function (billId, friendIds) {
        $.ajax({
            type: "Post",
            url: $("#btn_invite_offer").attr("data-url"),
            data: { billId: billId, FriendIds: friendIds },
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                    $(".shade").hide();
                    $(".mask_quote").hide();
                    //GY.Bill.Common.ShowSuccessTip();
                    window.location.href = window.location.href + "&isShowInvite=1";
                }
            }
        });
    },
    //显示成功提示
    ShowSuccessTip: function () {
        $("#div_success_tip").show();

        setTimeout(GY.Bill.Common.HideSuccessTip, 1000);
    },
    HideSuccessTip: function () {
        $("#div_success_tip").hide();
    },
    //正计时
    CountDown: function (time, obj) {
        var start_time = new Date(time).getTime(),
			sys_second = (new Date().getTime() - start_time) / 1000;
        var timer = setInterval(function () {
            if (sys_second < 24 * 60 * 60) {
                sys_second += 1;
                var day = Math.floor((sys_second / 3600) / 24);
                var hour = Math.floor((sys_second / 3600) % 24);
                var minute = Math.floor((sys_second / 60) % 60);
                var second = Math.floor(sys_second % 60);
                //$(obj).children(":eq(0)").text(day<10?"0"+day:day);//计算天
                $(obj).find("span").eq(0).text(hour < 10 ? "0" + hour : hour);//计算小时
                $(obj).find("span").eq(1).text(minute < 10 ? "0" + minute : minute);//计算分
                $(obj).find("span").eq(2).text(second < 10 ? "0" + second : second);// 计算秒
            } else {
                clearInterval(timer);
                $(obj).find("span").eq(0).text("24");//计算小时
                $(obj).find("span").eq(1).text("00");//计算分
                $(obj).find("span").eq(2).text("00");// 计算秒
            }
        }, 1000);
    },
    ValidDataResult: function () {
        var name = $("#txtName").val();
        var companyName = $("#txtCompanyName").val();
        var mobile = $("#txtMobile").val();
        var msg = "";
        if (name == "") {
            msg = "请输入姓名";
            return msg;
        }
        if (companyName == "") {
            msg = "请输入公司名";
            return msg;
        }
        if (mobile == "") {
            msg = "请输入手机号";
            return msg;
        }
        else {
            if (!(/^1[3|5|8|7][0-9]\d{8}$/.test(mobile))) {
                msg = "请输入正确的手机号";
                return msg;
            }
        }
        
        return msg;
    },
    SaveUser: function (callback) {
        var flag = false;
        var msg = GY.Bill.Common.ValidDataResult();
        if (msg != "") {
            $("#p_tip").text(msg);
            return flag;
        }

        var $this = $("#btnSaveUserOffer");
        $.ajax({
            type: "Post",
            url: $this.attr("data-url"),
            data: { Id: $this.attr("data-id"), Mobile: $("#txtMobile").val(), Name: $("#txtName").val(), CompanyName: $("#txtCompanyName").val() },
            dataType: "json",
            async:false,
            success: function (result) {
                if (result.code == 1) {
                    $(".shade").hide();
                    $(".mask").hide();
                    flag = true;
                }
            }
        });

        return flag;
    },
    SaveCheck: function (obj) {
        var isRegister = $(obj).attr("data-check");
        if (isRegister == 0) {
            $(".shade").hide();
            $(".mask_tip").hide();
            $("#div_mask_sign").show();
            return false;
        }
        return true;
    }
};

(function () {
    $(function () {
        //弹窗
        $(".mask").on("tap", ".close", function () {
            $(".shade").hide();
            $(".mask").hide();
        });

        $(".mask").on("tap", ".giveup", function () {
            $(".shade").hide();
            $(".mask").hide();
        }); 

        //选择好友
        $("#div_member_relation").on("tap", ".check", function () {
            $(this).toggleClass("active");
        });

        //好友分组切换
        $("#div_member_relation").on("tap", "span", function () {
            if ($(this).hasClass("active")) return;

            $(this).parent().find("span").removeClass("active");
            $(this).addClass("active");

            $("#div_member_relation").find(".list").hide();
            var groupId = $(this).attr("data-val");
            $("#div_group_" + groupId).show();
        });

        //邀请好友报价
        $("#btn_invite_offer").on("tap", function () {
            var billId = $("#div_member_relation").attr("data-id");
            var friendIds = [];
            $("#div_member_relation .list .active").each(function () {
                friendIds.push($(this).attr("data-val"));
            });
            if (friendIds.length == 0) {
                ShowTip("请选择好友");
                return;
            }
            GY.Bill.Common.InviteOffer(billId, friendIds);
        });
    });
})();
wx.ready(function () {
    wx.onMenuShareAppMessage({ title: shareTitle, desc: shareDesc, link: shareUrl, imgUrl: shareImg });
    wx.onMenuShareTimeline({title: shareTitle,link: shareUrl,imgUrl: shareImg});
});